<?php

/**
 * This is the model class for table "users".
 *
 * The followings are the available columns in table 'users':
 * @property integer $id
 * @property string $userid
 * @property string $password
 * @property string $salt
 * @property string $comments
 * @property string $createdBy
 * @property string $createdTime
 * @property string $updatedBy
 * @property string $updatedTime
 * @property integer $role
 *
 * The followings are the available model relations:
 * @property Userrole $role0
 * @property Userstaff[] $userstaffs
 */
class Users extends MedcoCActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Users the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	public function beforeSave() {
		//parent::beforeSave();
		$this->password = $this->hashPassword($this->password,$this->salt);
		return parent::beforeSave();
	}
	
    public function validatePassword($password)
    {
        return $this->hashPassword($password,$this->salt)===$this->password;
    }
 
    public function hashPassword($password,$salt)
    {
        return md5($salt.$password);
    }

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'users';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('userid, password, salt, role', 'required'),
			array('role', 'numerical', 'integerOnly'=>true),
			array('userid, createdBy, updatedBy', 'length', 'max'=>10),
			array('password, salt', 'length', 'max'=>128),
			array('comments', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, userid, role', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'role0' => array(self::BELONGS_TO, 'Userrole', 'role'),
			'userstaffs' => array(self::HAS_MANY, 'Userstaff', 'userId'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'userid' => 'Userid',
			'password' => 'Password',
			'salt' => 'Salt',
			'comments' => 'Comments',
			'createdBy' => 'Created By',
			'createdTime' => 'Created Time',
			'updatedBy' => 'Updated By',
			'updatedTime' => 'Updated Time',
			'role' => 'Role',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('userid',$this->userid,true);
		$criteria->compare('role',$this->role);

		//load the related table at the same time:
		$criteria->with=array('role0');
		
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}